package com.github.aloxc.plugin.restplus.listener;

import com.github.aloxc.plugin.restplus.common.Consts;
import com.github.aloxc.plugin.restplus.component.SettingForm;
import com.github.aloxc.plugin.restplus.entity.EnvironmentConfig;
import com.github.aloxc.plugin.restplus.entity.RestConfig;
import com.github.aloxc.plugin.restplus.entity.SettingFormType;
import com.github.aloxc.plugin.restplus.entity.SettingListenerContext;
import com.github.aloxc.plugin.restplus.i18n.I18Bundle;
import com.github.aloxc.plugin.restplus.navigator.RestServicesNavigator;
import com.intellij.openapi.util.IconLoader;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Map;
import java.util.stream.Collectors;

public class EnvironmentSettingMouseListener extends MouseAdapter {
    private SettingForm settingForm;

    public EnvironmentSettingMouseListener(SettingForm settingForm) {
        this.settingForm = settingForm;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        super.mouseClicked(e);
        super.mouseClicked(e);
        int x = e.getX();
        int y = e.getY();
        JPopupMenu menu = new JPopupMenu();        //创建菜单
        RestConfig rest = settingForm.getProject().getComponent(RestServicesNavigator.class).getConfig();
        Map<String, EnvironmentConfig> envMap = rest.getEnv();
        int index = 0;
        for (String envName : envMap.keySet().stream().sorted().collect(Collectors.toList())) {
            JMenuItem item = new JMenuItem(envName);//创建菜单项(点击菜单项相当于点击一个按钮)
            item.setIcon(IconLoader.getIcon("/icons/num/" + (index + 1) + ".png"));
            item.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            index++;
            menu.add(item);
            item.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    JMenuItem item = (JMenuItem) e.getSource();
                    item.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                    new SettingListener(settingForm, new SettingListenerContext(SettingFormType.ENVIRONMENT,I18Bundle.getMessage("com.github.aloxc.plugin.restplus.setting.environment.title") + "(" + item.getText() + ")"))
                            .actionPerformed(null);
                }
            });
        }
        JMenuItem item = new JMenuItem(I18Bundle.getMessage("com.github.aloxc.plugin.restplus.setting.environment.new"));//创建菜单项(点击菜单项相当于点击一个按钮)
        item.setIcon(Consts.Icons.NEW);
        item.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        item.addActionListener(ent -> {
            JMenuItem newItem = (JMenuItem) ent.getSource();
            newItem.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            new SettingListener(settingForm, new SettingListenerContext(SettingFormType.ENVIRONMENT, I18Bundle.getMessage("com.github.aloxc.plugin.restplus.setting.environment.new")))
                    .actionPerformed(null);
        });
        menu.add(item);
        menu.show(settingForm.getEnvironmentButton(), x, y);
    }
}
